package com.citycloud.ccuap.tc.yq.metatable;

import java.util.List;
import java.util.Map;

import com.citycloud.ccuap.tc.yq.constant.YqConstant;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.SelectProvider;
import org.springframework.cache.annotation.Cacheable;

import com.citycloud.ccuap.commons.db.SqlRestrictions;
import com.citycloud.ccuap.tc.yq.dto.TableDTO;
import com.citycloud.ccuap.tc.yq.entity.MetaField;
import com.citycloud.ccuap.tc.yq.entity.MetaTable;

@Mapper
public interface TableMapper {

	@SelectProvider(type = SqlProvider.class, method = "findByFieldLevel")
	@ResultMap("com.citycloud.ccuap.tc.yq.entity.MetaTable.ResultMapExt")
	public List<MetaTable> findByFieldLevel(String level,String datasource);
	
	@SelectProvider(type = SqlProvider.class, method = "findByFieldLevelNew")
	public List<Map<String,Object>> findByFieldLevelNew(String level, String datasource);

	@SelectProvider(type = SqlProvider.class, method = "findByFieldId")
	@ResultMap("com.citycloud.ccuap.tc.yq.entity.MetaTable.ResultMapExt")
	public MetaTable findByFieldId(String fieldId);

	@SelectProvider(type = SqlProvider.class, method = "findByDataGroupId")
	@ResultMap("com.citycloud.ccuap.tc.yq.entity.MetaTable.ResultMapExt")
	public List<MetaTable> findByDataGroupId(String groupId,String datasource);
	
	@SelectProvider(type = SqlProvider.class, method = "findByDataGroupIdNew")
	@ResultMap("com.citycloud.ccuap.tc.yq.entity.MetaField.ResultMapExt")
	public List<MetaField> findByDataGroupIdNew(String groupId, String datasource);
	
	@SelectProvider(type = SqlProvider.class, method = "getTable")
	@ResultType(value = String.class)
	public List<String> getTable(String userId, String projectId, String datasource);

	@SelectProvider(type = SqlProvider.class, method = "getTableByDatagroupId")
	@Cacheable(cacheNames="dataGroupTablesCache", key = "#p0")
	public List<TableDTO> getTableByDatagroupId(String datagroupId);
	
	class SqlProvider {
		public String findByFieldLevel(String level,String datasource) {
			StringBuffer sql = new StringBuffer(
					"SELECT DISTINCT atm_t.* FROM ap_t_meta_table_view AS atm_t LEFT JOIN ap_t_meta_field_view AS atm_f ON atm_t.OBJ_ID = atm_f.TABLE_ID WHERE 1 = 1");
			sql.append(SqlRestrictions.eq("atm_f.F_LEVEL", level));
			sql.append(SqlRestrictions.eq("atm_t.DB_ID", datasource));
			return sql.toString();
		}
		
		public String findByFieldLevelNew(String level,String datasource) {
			StringBuffer sql = new StringBuffer(
					"SELECT DISTINCT atm_t.obj_id objId, atm_f. LEVEL, atm_f.ename FROM ap_t_meta_table_view AS atm_t LEFT JOIN ap_t_meta_field_view AS atm_f ON atm_t.OBJ_ID = atm_f.TABLE_ID WHERE 1 = 1");
			sql.append(SqlRestrictions.eq("atm_f.F_LEVEL", level));
			sql.append(SqlRestrictions.eq("atm_t.DB_ID", datasource));
			return sql.toString();
		}

		public String findByFieldId(String fieldId) {
			StringBuffer sql = new StringBuffer(
					"SELECT DISTINCT atm_t.* FROM ap_t_meta_table_view AS atm_t LEFT JOIN ap_t_meta_field_view AS atm_f ON atm_t.OBJ_ID = atm_f.TABLE_ID WHERE 1 = 1");
			sql.append(SqlRestrictions.eq("atm_f.OBJ_ID", fieldId));
			return sql.toString();
		}

		public String findByDataGroupId(String groupId,String datasource) {
			StringBuffer sql = new StringBuffer(
					"SELECT amt.* FROM ap_t_meta_table_view as amt LEFT JOIN sys_dimension_table as ats_dt ON amt.OBJ_ID = ats_dt.table_id WHERE 1 = 1 ");
			sql.append(SqlRestrictions.eq("ats_dt.DATAGROUP_ID", groupId));
			if (!YqConstant.STR_NULL.equals(datasource) && StringUtils.isNotBlank(datasource)) {
				sql.append(SqlRestrictions.eq("amt.DB_ID", datasource));
			}
			return sql.toString();
		}
		
		public String findByDataGroupIdNew(String groupId,String datasource) {
			StringBuffer sql = new StringBuffer(
					"SELECT amf.* FROM ap_t_meta_field_view as amf INNER JOIN "
					+ "ap_t_meta_table_view as amt on amf.table_id=amt.obj_id LEFT JOIN "
					+ "sys_dimension_table as ats_dt ON amt.OBJ_ID = ats_dt.table_id WHERE 1 = 1 ");
			sql.append(SqlRestrictions.eq("ats_dt.datagroup_id", groupId));
			if (!YqConstant.STR_NULL.equals(datasource) && StringUtils.isNotBlank(datasource)) {
				sql.append(SqlRestrictions.eq("amt.DB_ID", datasource));
			}
			return sql.toString();
		}
		
		public String getTable(String userId, String projectId, String datasource){
			String  sql="select distinct table_id from v_user_tablefields where 1=1 ";
			sql+=SqlRestrictions.eq("user_id", userId);
			sql+=SqlRestrictions.eq("project_id", projectId);
			sql+=SqlRestrictions.eq("db_id", datasource);
			return sql;
		}
		
		public String getTableByDatagroupId(String datagroupId) {
			StringBuffer sql = new StringBuffer(
					"SELECT DISTINCT sdt.table_id tableId,sdt.ds_id dbId,sdt.table_config_id tableConfigId FROM sys_dimension_table_config AS sdt inner JOIN ap_t_meta_table_view AS t ON sdt.table_id = t.OBJ_ID WHERE 1 = 1");
			sql.append(SqlRestrictions.eq("sdt.datagroup_id", datagroupId));
			return sql.toString();
		}

	}


}
